/*
 * Copyright 2010 Cedric Priscal
 *
 * This file is part of stupid-avr-robot.
 *
 * stupid-avr-robot is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * stupid-avr-robot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with stupid-avr-robot.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include "Servo.h"

#define PERIOD 20000

#define EVENT_50HZ 0
#define EVENT_FINISH_PULSE 1

#define PORT PORTD
#define DDR DDRD

PropertyServoPosition::PropertyServoPosition() {
	PropertyU16::operator=(DEFAULT);
}

Servo::Servo(char pin) : Property(), mEnabled(this) {
    mPin = pin;
    DDR |= _BV(mPin);
}

void Servo::go(unsigned short position) {
	mPosition = position;
}

void Servo::onTimer(char what, unsigned short when) {
	if (mEnabled) {
		if (what == EVENT_50HZ) {
			/* 50Hz tick */
			/* start pulse, and program the timer to stop the pulse */
			PORT |= _BV(mPin);
			gTimer.schedule(this, EVENT_FINISH_PULSE, when + mPosition);
		} else {
			/* stop pulse */
			PORT &= ~_BV(mPin);
			gTimer.schedule(this, EVENT_50HZ, when + PERIOD - mPosition);
		}
	}
}

Property* Servo::getChild(unsigned char index) {
	switch(index) {
	case 0: return &mEnabled;
	case 1: return &mPosition;
	default: return 0;
	}
}

void Servo::onPropertyChanged(Property* prop) {
	if (mEnabled) {
        onTimer(EVENT_50HZ, gTimer.now());
    }
}
